﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vant.Properties;

namespace Vant.CellTypes
{
    public class SelectorDesigner : InputCellTypeBaseDesigner<Selector>, ISupportPropertyInitialize
    {
        public override void InitDefaultPropertyValues(IBuilderContext context)
        {
            base.InitDefaultPropertyValues(context);
            string[] defaults = Resources.Selector_defaultSetting.Split(',');
            foreach (var item in defaults)
            {
                this.CellType.options.Add(new NormalItem()
                {
                    label = item,
                    value = item,
                });
            }
        }
    }

    [Designer("Vant.CellTypes.SelectorDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Selector.png")]
    [OrderWeight((int)Utils.OrderWeight.Selector)]
    public class Selector : InputCellTypeBase 
    {
        public override List<Command> CommandList { get => base.CommandList; set => base.CommandList = value; }
        public override DataValidationLink DataValidationLink { get => base.DataValidationLink; set => base.DataValidationLink = value; }
        public override List<UIPermission> UIPermissions { get => base.UIPermissions; set => base.UIPermissions = value; }
        public override object DefaultValue { get => base.DefaultValue; set => base.DefaultValue = value; }

        [SRDisplayName(nameof(Resources.UseBinding))]
        [BoolProperty]
        [MergableProperty(false)]
        public bool useBinding { get; set; } = false;

        [ListProperty(IndentLevel = 1)]
        [SRDisplayName(nameof(Resources.Options))]
        public List<NormalItem> options { get; set; } = new List<NormalItem>();

        [SRBindingDataSourceProperty(Columns = nameof(Resources.Selector_BindingDataSourceProperty_Columns), IndentLevel = 1)]
        [SRDisplayName(nameof(Resources.BindingOptions))]
        public object bindingOptions { get; set; }

        [SRDisplayName(nameof(Resources.Selector_AllowAddEmptyItem))]
        [BoolProperty]
        public bool AllowAddEmptyItem { get; set; }

        [SRDisplayName(nameof(Resources.Selector_EmptyItemText))]
        public string EmptyItemLabel { get; set; }

        public override string placeholder { get => base.placeholder; set => base.placeholder = value; }
        public override ImageValue leftIcon { get => base.leftIcon; set => base.leftIcon = value; }
        public override ImageValue rightIcon { get => base.rightIcon; set => base.rightIcon = value; }
        public override bool border { get => base.border; set => base.border = value; }
        public override bool clearable { get => base.clearable; set => base.clearable = value; }
        public override bool ReadOnly { get => base.ReadOnly; set => base.ReadOnly = value; }
        public override bool IsDisabled { get => base.IsDisabled; set => base.IsDisabled = value; }

        [ObjectProperty(ObjType = typeof(SelectorConfig))]
        [SRDisplayName(nameof(Resources.Vant_PopupConfig))]
        public SelectorConfig SelectorConfig { get; set; } = new SelectorConfig();

        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if (propertyName == nameof(options))
            {
                return !useBinding;
            }
            if (propertyName == nameof(bindingOptions))
            {
                return useBinding;
            }
            if (propertyName == nameof(AllowAddEmptyItem))
            {
                return useBinding;
            }
            if (propertyName == nameof(EmptyItemLabel))
            {
                return useBinding && AllowAddEmptyItem;
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }
        [SRDisplayName(nameof(Resources.Vant_ShowPopup))]
        [RunTimeMethod]
        public void ShowPopup()
        {
        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.SetDataSourceByStringArray))]
        [SRDescription(nameof(Resources.SetDataSourceByStringArray_Description))]
        public void SetDataSourceByStringArray(
            [SRItemDisplayName(nameof(Resources.DataSource))]
            [Required]
            string dataSource)
        {

        }


        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.SetDataSourceByObjArray))]
        [SRDescription(nameof(Resources.SetDataSourceByObjArray_Description))]
        public void SetDataSourceByObjArray(
              [SRItemDisplayName(nameof(Resources.DataSource))]
                [Required]
                string dataSource,

                [SRItemDisplayName(nameof(Resources.ValueProperty))]
                [Required]
                string valueProperty = "value",

                [SRItemDisplayName(nameof(Resources.LabelProperty))]
                [SRDescription(nameof(Resources.SetDataSourceByIdPidTable_labelProperty_Description))]
                [Required]
                string labelProperty = "label")
        {

        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.GetDisplayText))]
        public GetSelectedTextResult GetSelectedText()
        {
            return null;
        }
        public class GetSelectedTextResult
        {
            [SRDisplayName(nameof(Resources.DisplayText))]
            public string SelectedText { get; set; }
        }

        public override string ToString()
        {
            return Properties.Resources.Selector;
        }
        public override SupportFeatures SupportFeatures => base.SupportFeatures | SupportFeatures.AllowSetFormat;

    }

    public class SelectorConfig : PickerConfigBase
    {
    }
    public class NormalItem
    {
        [SRDisplayName(nameof(Resources.Value))]
        public string value { get; set; }

        [SRDisplayName(nameof(Resources.Selector_NormalItem_Label))]
        public string label { get; set; }
    }
}
